<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<fieldset class="table-search-fieldset">
    <legend>搜索信息</legend>
    <div style="margin: 10px 10px 10px 10px">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">申请人姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="rmtPName" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">采购类别</label>
                    <div class="layui-input-inline">
                        <select name="rmtPType">
                            <option></option>
                            <option value='物料采购'>物料采购</option>
                            <option value="设备采购">设备采购</option>
                            <option value="杂物采购">杂物采购</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">申请部门</label>
                    <div class="layui-input-inline">
                        <select name="rmtPDepartment">
                            <option></option>
                            <option value="人事部">人事部</option>
                            <option value="销售部">销售部</option>
                            <option value="生产部">生产部</option>
                            <option value="财务部">财务部</option>
                            <option value="研发部">研发部</option>
                            <option value="物资部">物资部</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button id="searchbtn" type="submit" class="layui-btn layui-btn-primary" lay-submit
                            lay-filter="data-search-btn"><i
                            class="layui-icon"></i> 搜 索
                    </button>
                </div>
            </div>
        </form>
    </div>
</fieldset>


<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script>
    layui.use(['table', 'layer', 'form'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        form.render();
        table.render({
            elem: '#test'
            , url: '/purchasingapply'
            , toolbar: '#toolbarDemo'
            , title: '用户数据表'
            , totalRow: true
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'rmtPId', title: '单据编号', width: 120, edit: 'text'}
                , {field: 'rmtPDescription', title: '采购主题', width: 80, sort: true}
                , {field: 'rmtPType', title: '采购类别', width: 80, edit: 'text', sort: true}
                , {field: 'rmtPName', title: '申请人姓名', width: 100, sort: true}
                , {field: 'rmtPDepartment', title: '申请部门'}
                , {field: 'rmtPDate', title: '申请日期', width: 100}
                , {field: 'rmtPAddress', title: '到货地址', width: 120}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]]
            , page: true
        });

        //工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选')
                    break;
            }
            ;
        });

        //编辑删除
        table.on('tool(test)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if (layEvent === 'edit') {
                //do some
                layer.open({
                    title: '编辑采购申请单',
                    type: 1,
                    content: '111111'
                })
                //同步更新缓存对应的值
                obj.update({
                    username: '123'
                    , title: 'xxx'
                });
            } else if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                })
            }
        })

        // 监听搜索操作
        $("#searchbtn").click(function () {
            table.reload('test', {
                where: { //设定异步数据接口的额外参数，任意设
                    rmtPName: $("#rmtPName").val(),
                    rmtPType: $("#rmtPType").val(),
                    rmtPDepartment: $("#rmtPDepartment").val()
                }
                ,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            })
            ; //只重载数据
            return false;
        })
    });
</script>

</body>
</html>